﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace calc
{
    public partial class Calc : Form
    {
        private int count = 0;
        public Calc()
        {
            InitializeComponent();
        }

        private void ClickSeven(object sender, EventArgs e)
        {
            input.Text += seven.Text;
        }

        private void ClickDel(object sender, EventArgs e)
        {
            if (input.Text != null || cache.Text != "") {
                input.Text = input.Text.Substring(0, input.Text.Length - 1);
            }
        }

        private void ClickCE(object sender, EventArgs e)
        {
            input.Text = null;
        }

        private void ClickC(object sender, EventArgs e)
        {
            input.Text = null;
            cache.Text = null;
            count = 0;
        }

        private void ClickNegate(object sender, EventArgs e)
        {
            input.Text = "-" + input.Text;
        }

        private void ClickEight(object sender, EventArgs e)
        {
            input.Text += eight.Text;
        }

        private void ClickNine(object sender, EventArgs e)
        {
            input.Text += nine.Text;
        }

        private void ClickFour(object sender, EventArgs e)
        {
            input.Text += four.Text;
        }

        private void ClickOne(object sender, EventArgs e)
        {
            input.Text += one.Text;
        }

        private void ClickTwo(object sender, EventArgs e)
        {
            input.Text += two.Text;
        }

        private void ClickThree(object sender, EventArgs e)
        {
            input.Text += three.Text;
        }

        private void ClickZero(object sender, EventArgs e)
        {
            input.Text += zero.Text;
        }

        private void ClickDot(object sender, EventArgs e)
        {
            input.Text += ".";
        }

        private void ClickEqual(object sender, EventArgs e)
        {
            
        }

        private void ClickFive(object sender, EventArgs e)
        {
            input.Text += five.Text;
        }

        private void ClickSix(object sender, EventArgs e)
        {
            input.Text += six.Text;
        }

        private void SimpleCalc(String opera) {
            string pattern = @"[\+\-\*\/]";
            int firstNum = 0;
            int secondNum = 0;
            int result = 0;
            string usedOpera = Regex.Match(cache.Text, pattern).Value;

            if (count == 0)
            {
                count++;
                cache.Text = input.Text + " " + opera + " ";
                input.Text = null;
            }
            else {
                if (cache.Text.Substring(cache.Text.Length - 1) != " ") {
                    Boolean firstNumResult = int.TryParse(cache.Text.Substring(0, 1), out firstNum);
                    Boolean secondNumResult = int.TryParse(cache.Text.Substring(cache.Text.Length - 1), out secondNum);
                    if (firstNumResult && secondNumResult)
                    {
                        Console.WriteLine(firstNum);
                        Console.WriteLine(secondNum);
                        if (usedOpera == "+")
                        {
                            result = firstNum + secondNum;
                        }
                        else if (usedOpera == "-")
                        {
                            result = firstNum - secondNum;
                        }
                        else if (usedOpera == "*")
                        {
                            result = firstNum * secondNum;
                        }
                        else if (usedOpera == "/")
                        {
                            result = firstNum / secondNum;
                        }
                    }
                    cache.Text = result.ToString() + " " + opera + " " + input.Text;
                    input.Text = null;
                }
                else if(input.Text != null && input.Text != "")
                {
                    Boolean firstNumResult = int.TryParse(cache.Text.Substring(0, 1), out firstNum);
                    Boolean secondNumResult = int.TryParse(input.Text, out secondNum);
                    if (firstNumResult && secondNumResult)
                    {
                        Console.WriteLine(firstNum);
                        Console.WriteLine(secondNum);

                        if (usedOpera == "+")
                        {
                            result = firstNum + secondNum;
                        }
                        else if (usedOpera == "-")
                        {
                            result = firstNum - secondNum;
                        }
                        else if (usedOpera == "*")
                        {
                            result = firstNum * secondNum;
                        }
                        else if (usedOpera == "/")
                        {
                            result = firstNum / secondNum;
                        }
                    }
                    cache.Text = result.ToString() + " " + opera + " " + input.Text;
                    input.Text = null;
                }
            }
            
        }

        private void ClickMultiply(object sender, EventArgs e)
        {
            SimpleCalc(multiply.Text);
        }

        

        private void ClickAdd(object sender, EventArgs e)
        {
            SimpleCalc(add.Text);
        }

        private void ClickDivide(object sender, EventArgs e)
        {
            SimpleCalc(divide.Text);
        }

        private void ClickMinus(object sender, EventArgs e)
        {
            SimpleCalc(minus.Text);
        }

    }
}
